package com.lulu.baselib.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.android.material.snackbar.Snackbar;
import com.lulu.baselib.R;
import com.lulu.baselib.dialog.LoadingDialog;
import com.lulu.baselib.util.StatusBarUtil;
import com.lulu.baselib.util.StringUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Author: XuChang
 * Date: 2019/10/20 10:40
 * Email: 123286473@qq.com
 * Description:
 * History:
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected View mRootView;
    private CompositeDisposable mCompositeDisposable;

    protected abstract @LayoutRes
    int getLayoutId();

    protected abstract void initView();

    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarMode(this, true, R.color.white);
        if (getLayoutId() != -1) {
            setContentView(getLayoutId());
            mRootView = getWindow().getDecorView();
            ButterKnife.bind(this);
            initView();
        }
        if(registerEventBus()){
            EventBus.getDefault().register(this);
        }
    }


    protected boolean registerEventBus(){
        return false;
    }


    /**
     * 提示语
     *
     * @param view 当前activity中任意view即可
     * @param msg  弹出信息内容
     */
    protected void showSnack(View view, String msg) {
        Snackbar snackbar = Snackbar.make(view,
                msg, Snackbar.LENGTH_SHORT);
        View temp = snackbar.getView();
        temp.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        snackbar.show();
    }

    public void addDisposable(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
            mCompositeDisposable.clear();
            mCompositeDisposable = null;
        }
        if(EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

    //如果在onStop里dismiss会抛出异常 Can not perform this action after onSaveInstanceState，所以改成在onPause中dismiss
//    @Override
//    protected void onStop() {
////        dismissLoading();
//        super.onStop();
//    }

    @Override
    protected void onPause() {
        super.onPause();
        dismissLoading();
    }

    LoadingDialog loadingDialog;

    public void showLoading(String msg) {
        dismissLoading();
        loadingDialog = new LoadingDialog(msg);
        loadingDialog.setMsg(msg);
        loadingDialog.show(getSupportFragmentManager(), "loading");
    }

    public void showLoading() {
        showLoading("");
    }

    public void dismissLoading() {
        if (loadingDialog != null)
            loadingDialog.dismiss();
        loadingDialog = null;
    }


}
